package org.sdnstproject.interlayer.OF13Protocal;

import org.projectfloodlight.openflow.types.U16;
import org.projectfloodlight.openflow.types.U32;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class FuDescStatsRequest {
	private static final Logger log = LoggerFactory.getLogger(FuDescStatsRequest.class);
    public byte version = (byte)4;
    public byte type = (byte)18;
    public short length = (short)16;// fixed value property length = 16
    public long xid = 0x0L;
    public short statsType = (short)0;// fixed value property statsType = 0
    public int flags =(short) 0x1;
	
	public FuDescStatsRequest() {
		
	}
	
	public FuDescStatsRequest setXid(long xid) {
		this.xid = xid;
		return this;
	}
	
	public FuDescStatsRequest setField(String field, String value) {
		switch (field) {
		case "version":
			this.version = Byte.parseByte(value);
			break;
		case "type":
			this.type = Byte.parseByte(value);
			break;
		case "length":
			this.length = Short.parseShort(value);
			break;
		case "xid":
			this.xid = Long.parseLong(value);
			break;
		case "statsType":
			this.statsType = Short.parseShort(value);
			break;
		case "flags":
			this.flags = Integer.parseInt(value);
			break;
		}
		return this;
	}
	
	public ByteBuf writeTo() {
		ByteBuf bb = Unpooled.directBuffer();
		// fixed value property version = 4
        bb.writeByte(version);
        // fixed value property type = 18
        bb.writeByte(type);
        // fixed value property length = 16
        bb.writeShort(length);
        bb.writeInt(U32.t(xid));
        // fixed value property statsType = 0
        bb.writeShort(statsType);
        bb.writeShort(U16.t(flags));
        // pad: 4 bytes
        bb.writeZero(4);
        return bb;
    }
	
	@Override
    public String toString() {
        StringBuilder b = new StringBuilder("OFDescStatsRequestVer13(");
        b.append("version=").append(version);
        b.append(", ");
        b.append("type=").append(type);
        b.append(", ");
        b.append("length=").append(length);
        b.append(", ");
        b.append("xid=").append(xid);
        b.append(",");
        b.append("statsType=").append(statsType);
        b.append(",");
        b.append("flags=").append(flags);
        b.append(")");
        return b.toString();
    }
}
